package com.kaiwukj.android.ufamily.mvp.ui.page.home.home;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseFragment;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.mvp.helper.GlideEngine;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.SocialCircleActivity;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.publish.DynamicPublishFragment;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.message.MessageListFragment;
import com.kaiwukj.android.ufamily.mvp.ui.widget.KaiwuSlideTabLayout;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeContainerL1Fragment extends BaseFragment {

    public static final int COMMUNITY = 1;
    public static final int CITY = 2;

    @BindView(R.id.tab)
    KaiwuSlideTabLayout tabLayout;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    @BindView(R.id.iv_dynamic_add)
    View viewDynamicAdd;
    @BindView(R.id.iv_friend_add)
    View viewFriendAdd;

    @OnClick(R.id.iv_dynamic_add)
    void releaseDynamic() {
        ARouter.getInstance().build(ARouterUrlKt.SocialCircleUrl)
                .withString(SocialCircleActivity.FRAGMENT_KEY, DynamicPublishFragment.POST_CARD_TOPIC_FRAGMENT)
                .navigation();
    }

    @OnClick(R.id.iv_friend_add)
    void addFriend() {
        ARouter.getInstance()
                .build(ARouterUrlKt.MessageSearchUrl).navigation();
    }

    @OnClick(R.id.iv_dynamic_camera)
    void takeCamera() {
        // 用户已经同意该权限
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofAll())
                .isCompress(true)
                .videoQuality(75)
                .videoMaxSecond(15)
                .videoMinSecond(3)
                .maxVideoSelectNum(1)
                .maxSelectNum(9)
                .imageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.MyImagePickerStyle)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        ARouter.getInstance().build(ARouterUrlKt.SocialCircleUrl)
                                .withString(SocialCircleActivity.FRAGMENT_KEY, DynamicPublishFragment.POST_CARD_TOPIC_FRAGMENT)
                                .withString("media", GsonUtils.toJson(result))
                                .navigation();
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    @Override
    protected int getLayoutId() {
        QMUIStatusBarHelper.setStatusBarLightMode(getActivity());
        return R.layout.fragment_home_container_l1;
    }

    @Override
    protected void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeV20Fragment.newInstance(0));
        fragments.add(HomeV20Fragment.newInstance(1));
        fragments.add(HomeV20Fragment.newInstance(2));
        fragments.add(MessageListFragment.newInstance());

        /*List<String> titles = new ArrayList<>();
        titles.add("关注");
        titles.add("院子里");
        titles.add("同城");*/

        String[] titles = {"关注", "动态", "同城", "消息"};

        //  vpContent.setUserInputEnabled(false);
        vpContent.setOffscreenPageLimit(fragments.size());
     /*   vpContent.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });*/

        // tabLayout.setViewPager(vpContent,titles);
        tabLayout.setViewPager(vpContent, titles, getChildFragmentManager(), fragments);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
               /* if (position == 3) {
                    viewFriendAdd.setVisibility(View.VISIBLE);
                    viewDynamicAdd.setVisibility(View.GONE);
                } else {
                    viewFriendAdd.setVisibility(View.GONE);
                    viewDynamicAdd.setVisibility(View.VISIBLE);
                }*/
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 3) {
                    viewFriendAdd.setVisibility(View.VISIBLE);
                    viewDynamicAdd.setVisibility(View.GONE);
                } else {
                    viewFriendAdd.setVisibility(View.GONE);
                    viewDynamicAdd.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
/*
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = LayoutInflater.from(getContext())
                        .inflate(R.layout.weight_item_home_tab_selected, null);
                TextView title = view.findViewById(R.id.tv_title);
                title.setText(tab.getText());
                tab.setCustomView(view);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        new TabLayoutMediator(tabLayout, vpContent, true, (tab, position) -> {
            tab.setText(titles.get(position));
        }).attach();*/

        vpContent.setCurrentItem(1);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
    }
}
